package com.wsc.wscbirthdaykeeper.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
	public final static int MILISECONDS_IN_DAY = 1000 * 60 * 60 * 24;

	public static Date getDate(int year, int monthOfYear, int dayOfMonth) {
		Calendar date = GregorianCalendar.getInstance();
		date.set(Calendar.YEAR, year);
		date.set(Calendar.MONTH, monthOfYear);
		date.set(Calendar.DATE, dayOfMonth);
		return date.getTime();
	}

	public static Calendar getCalendarFromDate(Date date) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	public static int getYearFromDate(Date date) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	public static int getMonthFromDate(Date date) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH);
	}

	public static int getDateFromDate(Date date) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DATE);
	}

	public static int getWaitDays(Date date) {
		Calendar now = GregorianCalendar.getInstance();
		now.set(Calendar.HOUR, 0);
		now.set(Calendar.MINUTE, 0);
		now.set(Calendar.SECOND, 0);
		now.set(Calendar.MILLISECOND, 0);
		now.set(Calendar.HOUR_OF_DAY, 0);

		Calendar next = GregorianCalendar.getInstance();
		next.setTime(date);
		next.set(Calendar.HOUR, 0);
		next.set(Calendar.MINUTE, 0);
		next.set(Calendar.SECOND, 0);
		next.set(Calendar.MILLISECOND, 0);
		now.set(Calendar.HOUR_OF_DAY, 0);
		next.set(Calendar.YEAR, now.get(Calendar.YEAR));

		if (next.getTime().getTime() < now.getTime().getTime()) {
			next.set(Calendar.YEAR, now.get(Calendar.YEAR) + 1);
		}

		return getDaysBetweenDates(now.getTime(), next.getTime());
	}

	public static int getDaysBetweenDates(Date date1, Date date2) {
		return Math.abs((int) ((date1.getTime() - date2.getTime()) / MILISECONDS_IN_DAY));
	}

}
